/** 
 * Copyright (C), 2010-2012, AIYOU Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:SysUserServiceImp.java
 * Version:1.0
 * Date:Dec 10, 2011
 * Description: 
 * author:whh
 */
package com.tietiao.service.sysuser;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.tietiao.action.bg.form.BgUserForm;
import com.tietiao.common.constants.SysUserConstants;
import com.tietiao.common.exception.DataToManyException;
import com.tietiao.common.util.PageModel;
import com.tietiao.common.util.StringUtil;
import com.tietiao.dao.SysUserMapper;
import com.tietiao.dao.model.SysUser;
import com.tietiao.dao.model.SysUserCriteria;
import com.tietiao.service.bean.SysUserBean;

@Service
public class SysUserServiceImp implements SysUserService {
	@Resource
	private SysUserMapper sysUserMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tietiao.service.sysuser.SysUserService#deleteSysUserById(int)
	 */
	@Override
	public int deleteSysUserById(int id) {
		SysUser sysUser = loadSysUserById(id);
		if (sysUser != null) {
			sysUser.setIsActive(0);
			return sysUserMapper.updateByPrimaryKey(sysUser);
		} else {
			return 0;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tietiao.service.sysuser.SysUserService#getPageModel(int, int,
	 * com.tietiao.service.SysUserBean)
	 */
	@Override
	public PageModel<SysUser> getPageModel(int pageSize, int pageNum,
			SysUserBean userBean) {
		SysUserCriteria sysUserCriteria = new SysUserCriteria();
		SysUserCriteria.Criteria criteria = sysUserCriteria.createCriteria();

		pageNum = pageNum < 1 ? 1 : pageNum;// 最小值为1
		sysUserCriteria.setStart(pageSize * (pageNum - 1));
		sysUserCriteria.setLimit(pageSize * pageNum);
		if (StringUtil.checkStrIsNotNull(userBean.getPetName())) {
			criteria.andPetNameEqualTo(userBean.getPetName());
		}
		if (userBean.getGender() != null) {
			criteria.andGenderEqualTo(userBean.getGender());
		}
		if (StringUtil.checkStrIsNotNull(userBean.getEMail())) {
			criteria.andEmailEqualTo(userBean.getEMail());
		}
		if (userBean.getMaxCreateTime() != null) {
			criteria.andCreateTimeLessThanOrEqualTo(userBean.getMaxCreateTime());
		}
		if (userBean.getMinCreateTime() != null) {
			criteria.andCreateTimeGreaterThanOrEqualTo(userBean
					.getMinCreateTime());
		}
		if (StringUtil.checkStrIsNotNull(userBean.getRelName())) {
			criteria.andRelNameEqualTo(userBean.getRelName());
		}
		if (userBean.getIsActive() != null) {
			criteria.andIsActiveEqualTo(userBean.getIsActive());
		}
		sysUserCriteria.setOrderByClause(" id desc ");
		List<SysUser> list = sysUserMapper.selectByExample(sysUserCriteria);
		int count = sysUserMapper.countByExample(sysUserCriteria);
		return new PageModel<SysUser>(list, pageNum, pageSize, count);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.sysuser.SysUserService#insertSysUser(com.tietiao.
	 * dao.model.SysUser)
	 */
	@Override
	public int insertSysUser(SysUser sysUser) {
		sysUserMapper.insert(sysUser);
		return sysUser.getUserId();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.sysuser.SysUserService#loadSysUserByCodeOrEmail(java
	 * .lang.String)
	 */
	@Override
	public SysUser loadSysUserByCodeOrEmail(String str) {
		SysUserCriteria sysUserCriteria = new SysUserCriteria();
		SysUserCriteria.Criteria criteria = sysUserCriteria.createCriteria();
		if (StringUtil.strIsNull(str)) {
			return null;
		} else if (StringUtil.isEmailAdress(str)) {
			criteria.andEmailEqualTo(str);
		} else {
			criteria.andPetNameEqualTo(str);
		}
		criteria.andIsActiveEqualTo(SysUserConstants.ACTIVE_USABLE);
		List<SysUser> list = sysUserMapper.selectByExample(sysUserCriteria);
		if (list == null || list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tietiao.service.sysuser.SysUserService#loadSysUserById(int)
	 */
	@Override
	public SysUser loadSysUserById(int id) {
		return sysUserMapper.selectByPrimaryKey(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.sysuser.SysUserService#updateSysUser(com.tietiao.
	 * dao.model.SysUser)
	 */
	@Override
	public int updateSysUser(SysUser sysUser) {
		return sysUserMapper.updateByPrimaryKey(sysUser);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.sysuser.SysUserService#loadAdminByPetName(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public SysUser loadAdminByPetName(String petName, String password)
			throws DataToManyException {
		SysUserCriteria sysUserCriteria = new SysUserCriteria();
		SysUserCriteria.Criteria criteria = sysUserCriteria.createCriteria();

		criteria.andPetNameEqualTo(petName);
		criteria.andPasswordEqualTo(password);
		criteria.andIsActiveEqualTo(SysUserConstants.ACTIVE_USABLE);
		criteria.andTypeEqualTo(SysUserConstants.TYPE_ADMIN);

		List<SysUser> list = sysUserMapper.selectByExample(sysUserCriteria);
		if (list == null || list.size() == 0) {
			return null;
		} else if (list.size() == 1) {
			return list.get(0);
		} else {
			throw new DataToManyException();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tietiao.service.sysuser.SysUserService#selectFriendsUser(int,
	 * int, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<SysUser> selectFriendsUser(int start, int limit,
			Integer userId, Integer sysUserId,String petName, Integer groupId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", start);
		map.put("limit", limit);
		if (userId != null) {
			map.put("userId", userId);
		} else {
			map.put("sysUserId", sysUserId);
		}
		if(StringUtils.isNotBlank(petName)){
			map.put("petName", petName);
		}
		map.put("groupId", groupId);
		return sysUserMapper.selectFriendsUser(map);
	}

	public int selectFriendsUserCount(Integer userId, Integer sysUserId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (userId != null) {
			map.put("userId", userId);
		} else {
			map.put("sysUserId", sysUserId);
		}
		return sysUserMapper.selectFriendsUserCount(map);
	}

	@Override
	public PageModel<SysUser> getUserPageModel(BgUserForm bgUserForm,
			int start, int limit, String orderName, String orderType) {

		SysUserCriteria sysUserCriteria = new SysUserCriteria();
		SysUserCriteria.Criteria criteria = sysUserCriteria.createCriteria();

		sysUserCriteria.setStart(start);
		sysUserCriteria.setLimit(limit);
		if (bgUserForm != null) {
			criteria.andPetNameEqualTo(bgUserForm.getPetName());
			criteria.andGenderEqualTo(bgUserForm.getGender());
			criteria.andEmailLike((bgUserForm.getEmail()));
			criteria.andCreateTimeLessThanOrEqualTo(bgUserForm
					.getCreateTimeEnd());
			criteria.andCreateTimeGreaterThanOrEqualTo(bgUserForm
					.getCreateTimeStart());
			criteria.andRelNameEqualTo(bgUserForm.getRelName());
			criteria.andIsActiveEqualTo(bgUserForm.getIsActive());
			criteria.andTypeEqualTo(bgUserForm.getType());
		}
		sysUserCriteria.setOrderByClause(StringUtil.wrapSort(orderName,
				orderType));
		List<SysUser> list = sysUserMapper.selectByExample(sysUserCriteria);
		int count = sysUserMapper.countByExample(sysUserCriteria);

		return new PageModel<SysUser>(list, count);
	}

	public int batchUpTypeOrActives(Integer type, Integer isActive,
			String password, String userIds) {
		return sysUserMapper.batchUpTypeOrActives(type, isActive,password, userIds);
	}

	public List<SysUser> selectAttHotLable(String lableName) {
		return sysUserMapper.selectAttHotLable(lableName);
	}
	public SysUser getSysUserByWebService(String openId) {
		if(StringUtil.strIsNull(openId)){
			return null;
		}else{
			SysUserCriteria sysUserCriteria = new SysUserCriteria();
			SysUserCriteria.Criteria criteria = sysUserCriteria.createCriteria();
			criteria.andColumn4EqualTo(openId);
			criteria.andIsActiveEqualTo(SysUserConstants.ACTIVE_USABLE);
			List<SysUser> list = sysUserMapper.selectByExample(sysUserCriteria);
			
			if (list == null || list.size() == 0) {
				return null;
			} else {
				return list.get(0);
			}
		}
	}
	
	@Override
	public List<SysUser> searchSysUsersByPetName(String petName,
			Integer loginUserId, Integer start, Integer limit,
			String orderByClause) {
		return sysUserMapper.selectUserByPetName(petName, loginUserId, start, limit, orderByClause);
	}

}
